// 问卷调查
import React, { useReducer, useState } from "react";
import queryApi from "../../api/query";

const QuestionnaireContext = React.createContext();

const dataReducer = (state, action) => {
  switch (action.type) {
    case "new_ques":
      return {
        _id: state._id,
        title: "",
        target: action.payload.index,
        body: [],
      };
    case "add_ques":
      return {
        _id: state._id,
        title: "",
        target: state.target,
        body: [...state.body, action.payload.newData],
      };
    case "edit_group_ques":
      return {
        _id: state._id,
        title: "",
        target: state.target,
        body: action.payload.newBody,
      };
    case "delete_group_ques":
      return state.filter((item, index) => index !== action.payload.index);
    case "get_ques":
      return {
        ...action.payload.ques,
      };
    default:
      return state;
  }
};

let initData = {
  target: -1, // 0：一般户，1：贫困户，2：脱贫户
  title: "",
  body: [],
};

export const QuestionnaireProvider = ({ children }) => {
  const [QuesData, dispatch] = useReducer(dataReducer, initData);
  const [quesLoading, setQuesLoading] = useState("end");

  const newQues = (index) => {
    dispatch({ type: "new_ques", payload: { index } });
  };
  const addQues = (newData) => {
    dispatch({ type: "add_ques", payload: { newData } });
    console.log("新增题目啦---", QuesData);
  };
  const editGroupQues = (newBody) => {
    dispatch({ type: "edit_group_ques", payload: { newBody } });
    // console.log("修改题目组啦---", QuesData.body[0].contents[0].visible);
  };
  const deleteGroup = (groupIndex) => {
    dispatch({ type: "delete_group_ques", groupIndex: groupIndex });
  };

  const getQues = async () => {
    try {
      setQuesLoading("ing");
      // let time = new Date();
      // console.log("时间1---", time.getMinutes() + ":" + time.getSeconds());
      let data = await queryApi.get("ques");
      console.log("查找问卷模板成功---", data.data.status);
      // let time4 = new Date();
      // console.log("时间4---", time4.getMinutes() + ":" + time4.getSeconds());
      await dispatch({ type: "get_ques", payload: { ques: data.data.data } });
      // let time5 = new Date();
      // console.log("时间5---", time5.getMinutes() + ":" + time5.getSeconds());
      setQuesLoading("end");
    } catch (error) {
      setQuesLoading("error");
      console.log("查找问卷模板失败---", error);
    }
  };
  const postNewQues = async () => {
    try {
      let data = await queryApi.post("ques", QuesData);
      console.log("添加问卷模板成功---", data.data.status);
    } catch (error) {
      console.log("添加问卷模板失败---", error);
    }
  };

  const updateQues = async () => {
    try {
      // console.log(QuesData);
      await queryApi.patch("ques/" + QuesData._id, QuesData);
      let data = await queryApi.get("ques");
      console.log("修改问卷模板成功---", data.data.status);
      dispatch({ type: "get_ques", payload: { ques: data.data.data } });
    } catch (error) {
      console.log("修改问卷模板失败---", error);
    }
  };

  return (
    <QuestionnaireContext.Provider
      value={{
        quesLoading,
        QuesData,
        addQues,
        newQues,
        editGroupQues,
        getQues,
        postNewQues,
        updateQues,
      }}
    >
      {children}
    </QuestionnaireContext.Provider>
  );
};

export default QuestionnaireContext;
